﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DA;
using System.Text.RegularExpressions;
using Entities;

namespace BL
{
    public class RoleManager
    {
        public IEnumerable<ERP_Roles> GetAllRoles()
        {
            using (dotNetivotEntities model = new dotNetivotEntities())
            {
                return model.ERP_Roles.ToList();
            }
        }

        public ERP_Roles Getrole(int id)
        {
            using (dotNetivotEntities model = new dotNetivotEntities())
            {
                return model.ERP_Roles.SingleOrDefault(x => x.RoleId == id);
            }
        }

        public bool EditRole(ERP_Roles role)
        {
            try
            {
                using (dotNetivotEntities model = new dotNetivotEntities())
                {
                    ERP_Roles roleToUpdate = model.ERP_Roles.SingleOrDefault(x => x.RoleId == role.RoleId);
                    roleToUpdate.Name = role.Name;
                    roleToUpdate.Permmisions = role.Permmisions;
                    roleToUpdate.RoleDescription = role.RoleDescription;

                    model.SaveChanges();
                }
            }
            catch
            {
                return false;
            }

            return true;
        }

        public bool AddRole(ERP_Roles role)
        {
            try
            {
                using (dotNetivotEntities model = new dotNetivotEntities())
                {
                    model.ERP_Roles.AddObject(role);
                    model.SaveChanges();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool RoleExist(int id)
        {
            using (dotNetivotEntities model = new dotNetivotEntities())
            {
                return model.ERP_Roles.Any(x => x.RoleId == id);
            }
        }
    }
}
